package vcp.client.GUI.mvc.controller;

import javax.swing.SwingUtilities;

import vcp.client.GUI.events.FormSubmitionEvent;
import vcp.client.GUI.mvc.model.UIAbstractModel;
import vcp.client.GUI.mvc.model.UIQuarterReportsModel;
import vcp.client.GUI.mvc.view.UIAbstractView;
import vcp.client.GUI.mvc.view.UIQuarterReportsView;
import vcp.client.forms.PeriodicForm;
import vcp.client.interfaces.listeners.IFormSubmitionListener;
import vcp.client.utility.Common.UserNotification;
import vcp.entity.Worker;
import vcp.enums.PopUpMessageType;
import vcp.enums.Screen;

/**
 * 
 * @author VCP team
 *         <p>
 *         Controller class responsible of getting and setting reports on screen
 *         according users demand
 *         </p>
 * @see UIQuarterReportsView
 * @see UIQuarterReportsModel
 */
public class UIQuarterReportsController extends UIAbstractController implements IFormSubmitionListener <PeriodicForm>
{
	/** reference to controller's view **/
	private final UIQuarterReportsView view;

	/** reference to controller's model **/
	private final UIQuarterReportsModel model;

	/**
	 * @param view
	 *            to present on screen
	 * @param model
	 *            data source
	 */
	public UIQuarterReportsController ( UIQuarterReportsView view, UIQuarterReportsModel model )
	{
		// notifying type of MVC for the Root controller
		super(Screen.GEN_REPORTS);
		this.view = view;
		this.model = model;

		// delegating self to view and model
		this.setupListeners();
	}

	// ****************************************************************************************************************************
	// UIAbstractController methods
	// ****************************************************************************************************************************
	@Override
	public UIAbstractView getView ()
	{
		return this.view;
	}

	@Override
	public UIAbstractModel getModel ()
	{
		return this.model;
	}

	@Override
	public void clearMVC ()
	{
		this.view.setOnScreen(false);
	}

	@Override
	public void flashData ()
	{
		this.model.clearModel();
		this.view.clearView();
	}

	@Override
	public void uploadDataContext ( Object data )
	{
		if ( data instanceof Worker )
		{
			Worker siteManager = (Worker) data;

			this.model.setSiteManager(siteManager);
			this.view.setOnScreen(true);
		}
	}

	@Override
	protected synchronized void reloadGUI ()
	{
		this.view.loadReport(this.model.getReport());
	}

	@Override
	protected void setupListeners ()
	{
		super.setupListeners();
		this.view.setFormSubmitionListener(this);
	}

	// ****************************************************************************************************************************
	// IControllerModelDelegate methods ***
	// ****************************************************************************************************************************
	@Override
	public void updateControllerDelegateWithData ( Object newData )
	{
		if ( newData instanceof Boolean )
		{
			this.parentDelegate.showPopUpMessage(UserNotification.FORM_SUBMITTED, PopUpMessageType.INFORMATION);
		}

		SwingUtilities.invokeLater(new Runnable()
			{
				@Override
				public void run ()
				{
					UIQuarterReportsController.this.reloadGUI();
				}
			});
	}

	// ****************************************************************************************************************************
	// IControllerViewDelegate methods ***
	// ****************************************************************************************************************************
	@Override
	public void onButtonPress ( Object object )
	{
		this.model.savePeriodicReport();
	}

	// ****************************************************************************************************************************
	// IFormSubmitionListener methods ***
	// ****************************************************************************************************************************
	@Override
	public void onFormSubmitted ( FormSubmitionEvent <PeriodicForm> event )
	{
		this.model.retrievePeriodicReport(event.getForm());
	}
}
